package com.hunttown.mes.convert;

import com.google.common.collect.Lists;
import com.hunttown.mes.common.utils.BeanUtils;
import com.hunttown.mes.domain.TransportPurchaseDetail;
import com.hunttown.mes.rpc.domain.TransportPurchaseDetailDTO;
import com.hunttown.mes.rpc.exception.ConvertException;
import com.hunttown.mes.rpc.domain.enums.ErrorCode;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.List;

/**
 * created by wangjunfu on 2019-05-18 20:13:43
 */
public class TransportPurchaseDetailConverts {

    private static Logger LOG = LoggerFactory.getLogger(TransportPurchaseDetailConverts.class);

    public static List<TransportPurchaseDetailDTO> convert(List<TransportPurchaseDetail> transportPurchaseDetailList) {
        if (CollectionUtils.isEmpty(transportPurchaseDetailList)) {
            return Collections.EMPTY_LIST;
        }

        List<TransportPurchaseDetailDTO> transportPurchaseDetailListResult = Lists.newArrayList();
        for (TransportPurchaseDetail transportPurchaseDetail : transportPurchaseDetailList) {
            transportPurchaseDetailListResult.add(convert(transportPurchaseDetail));
        }
        return transportPurchaseDetailListResult;
    }

    public static TransportPurchaseDetailDTO convert(TransportPurchaseDetail transportPurchaseDetail) {
        try {
            return BeanUtils.copy(transportPurchaseDetail, TransportPurchaseDetailDTO.class);
        } catch (Exception e) {
            LOG.error("TransportPurchaseDetail到TransportPurchaseDetailDTO转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "TransportPurchaseDetail到TransportPurchaseDetailDTO转换失败", e);
        }
    }

    public static TransportPurchaseDetail convert(TransportPurchaseDetailDTO transportPurchaseDetailDTO) {
        try {
            return BeanUtils.copy(transportPurchaseDetailDTO, TransportPurchaseDetail.class);
        } catch (Exception e) {
            LOG.error("TransportPurchaseDetailDTO到TransportPurchaseDetail转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "TransportPurchaseDetailDTO到TransportPurchaseDetail转换失败", e);
        }
    }
}